package com.rjkj.medicalwaste.uncaught;

import android.content.Context;


public class AppExceptionHandle implements Thread.UncaughtExceptionHandler{
    private Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
    private static AppExceptionHandle appExceptionHandle = new AppExceptionHandle();
    private Context context;
    private ExceptionListener  exceptionListener;
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        exceptionListener.before();
        boolean fix = exceptionListener.fix(t,e,context);
        exceptionListener.after();
        if(!fix){
            uncaughtExceptionHandler.uncaughtException(t,e);
        }
    }
    public ExceptionListener getExceptionListener() {
        return exceptionListener;
    }
    private AppExceptionHandle(){
        
    }

    public static synchronized AppExceptionHandle getInstance() {
        if(appExceptionHandle==null){
            return new AppExceptionHandle();
        }
        return appExceptionHandle;
    }

    public void init(Context context, ExceptionListener exceptionListener){
        this.uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
        this.context = context; //关联上下文
        this.exceptionListener = exceptionListener;
    }
}
